﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcXRepairAdmin.Interfaces;
using MvcXRepairAdmin.Services;
using XRepair.Models;

namespace MvcXRepairAdmin.Controllers
{
    public class BrandController : BaseController
    {
        private IBrandService _brandService;

        public BrandController()
        {
            _brandService = new BrandService();
        }

        public ActionResult Index()
        {
            IEnumerable<Brand> brandList = _brandService.GetAllActiveBrands();

            List<Brand> model = brandList != null ? brandList.ToList() : new List<Brand>();

            return View(model);
        }

        public ActionResult Delete(int id)
        {
            bool succeed = _brandService.DeactivateBrandByID(id);

            if (succeed)
            {
                return RedirectToAction("Index");
            }

            return RedirectToAction("ErrorOnProcessing", "Error");
        }

        public ActionResult AddNew(Brand model)
        {
            model.CreatedBy = _userSession.Username;

            bool succeed = _brandService.AddNewBrand(model);

            if (succeed)
            {
                return RedirectToAction("Index");
            }

            return RedirectToAction("ErrorOnProcessing", "Error");
        }

        public ActionResult Edit(Brand model)
        {
            model.LastUpdatedBy = _userSession.Username;

            bool succeed = _brandService.UpdateBrand(model);

            if (succeed)
            {
                return RedirectToAction("Index");
            }

            return RedirectToAction("ErrorOnProcessing", "Error");
        }
    }
}
